package com.example.demo.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.*;
import java.text.DecimalFormat;
import java.util.Date;

public class TestUtil {
//    private static final double GB_DIVISOR = 1024.0 * 1024.0 * 1024.0;
//    public static String formatBytesToGB(long bytes) {
//        if (bytes == 0) return "0.00";
//        BigDecimal bd = new BigDecimal(bytes)
//                .divide(new BigDecimal(GB_DIVISOR), 2, RoundingMode.HALF_UP);
//        return bd.toPlainString(); // 确保不使用科学计数法
//    }

    private static final BigDecimal GB_DIVISOR = new BigDecimal(1024 * 1024 * 1024);

    public static String formatBytesToGB(long bytes) {
        if (bytes == 0) return "0.00";
        return new BigDecimal(bytes)
                .divide(new BigDecimal(1024 * 1024 * 1024), 2, RoundingMode.HALF_UP)
                .toPlainString();
    }
    public static void main(String[] args) {

        String postUrl = "https://10.3.39.93:8888/tv2cms";
        String xmlString = "<xml><test>test</test></xml>";

        try {
            String response = sendPostUtf8Request(postUrl, xmlString);
            // 处理成功响应
        } catch (IOException e) {
            if (e.getMessage().contains("网络连接")) {
                // 处理网络连接错误（显示给用户）
                System.err.println("网络连接问题: " + e.getMessage());
            } else {
                // 处理其他IO异常（如服务器错误）
                System.err.println("服务器异常: " + e.getMessage());
            }
        }

//        try{
//            System.out.println("11111");
//        }catch (Exception e){
//            System.out.println("错误11111");
//        }
//        try{
//            System.out.println("222222");
////            int i = 1/0;
//            //HttpClientUtils.postJson("http://www.ddd.com","faf");
//        }catch (Exception e){
//            System.out.println("错误22222");
//        }
//        try{
//            System.out.println("3333333");
//        }catch (Exception e){
//            System.out.println("错误3333333");
//        }
        Date today = new Date();
//        if (!PublicUtils.isEmptyString(day)){
//            today = DateUtils.strToDate(day, DateUtils.DEFAULT_DATE_FORMAT);
//        }
//        today = DateUtil.strToDat2("2025-05-09 00:00:00", "yyyy-MM-dd");
        System.out.println(today);
    }

    private static String sendPostUtf8Request(String url, String xmlData) throws IOException {
        HttpURLConnection conn = null;
        try {
            URL requestUrl = new URL(url);
            conn = (HttpURLConnection) requestUrl.openConnection();
            // 设置连接超时和读取超时（毫秒）
            conn.setConnectTimeout(5000); // 连接超时时间
            conn.setReadTimeout(10000);   // 读取超时时间

            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/xml; charset=UTF-8");
            conn.setDoOutput(true);

            try (OutputStream os = conn.getOutputStream()) {
                byte[] input = xmlData.getBytes("UTF-8");
                os.write(input, 0, input.length);
            }

            int responseCode = conn.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 获取响应头中的 Content-Type 字段
                String contentType = conn.getContentType();
                String charset = "UTF-8";
                if (contentType != null && contentType.contains("charset=")) {
                    // 从 Content-Type 中提取编码信息
                    int charsetIndex = contentType.indexOf("charset=");
                    charset = contentType.substring(charsetIndex + 8);
                }

                try (BufferedReader br = new BufferedReader(
                        new InputStreamReader(conn.getInputStream(), charset))) {
                    StringBuilder response = new StringBuilder();
                    String responseLine;
                    while ((responseLine = br.readLine()) != null) {
                        response.append(responseLine.trim());
                    }
                    return response.toString();
                }
            } else {
                throw new IOException("HTTP error code: " + responseCode);
            }
        } catch (ConnectException e) {
            // 网络连接失败（例如服务器不可达、URL错误）
            throw new IOException("网络连接错误: 无法连接到服务器 " + url, e);
        } catch (SocketTimeoutException e) {
            // 连接超时或读取超时
            throw new IOException("网络连接超时: 服务器响应超时 " + url, e);
        } catch (UnknownHostException e) {
            // 域名解析失败
            throw new IOException("网络错误: 无法解析主机名 " + url, e);
        } catch (IOException e) {
            // 其他IO异常
            throw new IOException("发送请求失败: " + e.getMessage(), e);
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
    }
}
